<?php
/**
 * 文章数据操作类
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/17
 * Time: 10:04
 */

namespace backend\services;


use common\helps\Func;
use common\services\BasicService;
use Yii;
use yii\helpers\ArrayHelper;

class ArticleService extends BasicService
{
    /**
     * 保存之前的数据操作
     * @param $data
     * @param int $mode
     * @param array $oldData
     * @return array
     */
    public function beforeSave(&$data, $mode = self::CREATE, $oldData = [])
    {
        $data['description'] = $data['description'] ? $data['description'] : Func::moreHidden($data['content'], 0, 60);
        if (!isset($data['art_id']) || !$data['art_id']) {
            $data['create_time'] = time();//创建时间
            $userInfo = Yii::$app->session->get('admin');
            $data['create_id'] = $userInfo['id'];//创建人
        }

        return $this->returnMsg('数据处理成功', true);
    }

    /**
     * 列表页数据处理
     * @param $info
     * @param array $col
     * @return array
     */
    public function tableHanding($info, $col = [])
    {
        $info['create_time'] = $info['create_time'] ? date('Y-m-d H:i:s', $info['create_time']) : '';//创建时间格式化
        $info['cat_id'] = ArrayHelper::getValue((new CategoryService())->getInfoByPk($info['cat_id']), 'name');//获取分类名称
        $res = [];
        //没有则直接按顺序直接返回数据
        foreach($col as $field){
            if(isset($info[$field])){
                $res[] = $info[$field];
            } else if($field == '_'){
                $res[] = $this->getTableActions($info);
            } else {
                $res[] = '';
            }
        }

        return $res;
    }

}